package com.mangocheese.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.mangocheese.subject.common.enums.IsDeletedFlagEnum;
import com.mangocheese.subject.domain.convert.SubjectInfoConverter;
import com.mangocheese.subject.domain.entity.SubjectInfoBO;
import com.mangocheese.subject.domain.handler.SubjectTypeHandler;
import com.mangocheese.subject.domain.handler.SubjectTypeHandlerFactory;
import com.mangocheese.subject.domain.service.SubjectInfoDomainService;
import com.mangocheese.subject.infra.basic.entity.SubjectInfo;
import com.mangocheese.subject.infra.basic.entity.SubjectMapping;
import com.mangocheese.subject.infra.basic.service.SubjectInfoService;
import com.mangocheese.subject.infra.basic.service.SubjectMappingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private SubjectMappingService subjectMappingService;
//
//    @Resource
//    private SubjectLabelService subjectLabelService;
//
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectInfoDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectInfoBO));
        }
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convert(subjectInfoBO);
        subjectInfo.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectInfoService.insert(subjectInfo);

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());
        handler.add(subjectInfoBO);
        List<SubjectMapping> mappingList = new LinkedList<>();
        subjectInfoBO.getCategoryIds().forEach(categoryId -> subjectInfoBO.getLabelIds().forEach(labelId -> {
            SubjectMapping subjectMapping = new SubjectMapping();
            subjectMapping.setSubjectId(subjectInfo.getId());
            subjectMapping.setCategoryId(categoryId);
            subjectMapping.setLabelId(labelId);
            subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            mappingList.add(subjectMapping);
        }));
        subjectMappingService.batchInsert(mappingList);
    }


//    @Override
//    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
//        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
//        pageResult.setPageNo(subjectInfoBO.getPageNo());
//        pageResult.setPageSize(subjectInfoBO.getPageSize());
//        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
//        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToInfo(subjectInfoBO);
//        int count = subjectInfoService.countByCondition(subjectInfo, subjectInfoBO.getCategoryId()
//                , subjectInfoBO.getLabelId());
//        if (count == 0) {
//            return pageResult;
//        }
//        List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo, subjectInfoBO.getCategoryId()
//                , subjectInfoBO.getLabelId(), start, subjectInfoBO.getPageSize());
//        List<SubjectInfoBO> subjectInfoBOS = SubjectInfoConverter.INSTANCE.convertListInfoToBO(subjectInfoList);
//        subjectInfoBOS.forEach(info->{
//            SubjectMapping subjectMapping = new SubjectMapping();
//            subjectMapping.setSubjectId(info.getId());
//            List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
//            List<Long> labelIds = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//            List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIds);
//            List<String> labelNames = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
//            info.setLabelName(labelNames);
//        });
//        pageResult.setRecords(subjectInfoBOS);
//        pageResult.setTotal(count);
//        return pageResult;
//    }

//    @Override
//    public SubjectInfoBO queryInfo(SubjectInfoBO subjectInfoBO) {
//        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBO.getId());
//        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
//        SubjectOptionBO optionBO = handler.query(subjectInfo.getId().intValue());
//        SubjectInfoBO bo = SubjectInfoConverter.INSTANCE.convertOptionAndInfoToBo(optionBO, subjectInfo);
//        SubjectMapping subjectMapping = new SubjectMapping();
//        subjectMapping.setSubjectId(subjectInfo.getId());
//        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
//        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
//        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
//        bo.setLabelName(labelNameList);
//        return bo;
//    }


}
